#include "iostream"
#include "stdlib.h"
 
#include "cv.h"
#include "highgui.h"
IplImage * resz(IplImage *);
typedef struct{
     int x;
     int y;
     int width;   
     int height;
}GOZ;
int main(){
     CvSize imgSize;
     imgSize.width = 640;
     imgSize.height = 480;
     IplImage* gr = cvCreateImage( imgSize, IPL_DEPTH_8U, 1);
     

     IplImage * resim;
     IplImage * mo;    
     int w,h;
     resim = cvLoadImage("lc.jpg",1);
     gr=cvLoadImage("mask.jpg",0);
     mo=cvLoadImage("mouth.jpg",0);
   CvRect bndRect = cvRect(0,0,0,0);
   CvRect mRect = cvRect(0,0,0,0);
   CvMemStorage* storage = cvCreateMemStorage(0);
   CvSeq* contour = 0;
   CvSeq* contour1 = 0;
   cvFindContours( gr, storage, &contour, sizeof(CvContour), CV_RETR_CCOMP, CV_CHAIN_APPROX_SIMPLE );
   cvFindContours( mo, storage, &contour1, sizeof(CvContour), CV_RETR_CCOMP, CV_CHAIN_APPROX_SIMPLE );
   CvPoint pt1, pt2;
   CvPoint ma1, ma2;
      CvPoint ptm1, ptm2;
   CvPoint mam1, mam2;
   ma1.x=1;
   ma2.x=1;
   ma1.y=1;
   ma2.y=1;
      mam1.x=1;
   mam2.x=1;
   mam1.y=1;
   mam2.y=1;
   for( ; contour1 != 0; contour1 = contour1->h_next ){
   mRect = cvBoundingRect(contour1, 0);
   ptm1.x = mRect.x;
   ptm1.y = mRect.y;
   ptm2.x = mRect.x + mRect.width;
   ptm2.y = mRect.y + mRect.height;
   if(abs(ptm1.x-ptm2.x)>abs(mam1.x-mam2.x)){
   mam1.x=ptm1.x;
   mam2.x=ptm2.x;
   w=mRect.width;
    // if(abs(ptm1.y-ptm2.y)>abs(mam1.y-mam2.y)){
      mam1.y=ptm1.y;
      mam2.y=ptm2.y;
      h=mRect.height;
    // }
   }
//cvRectangle(resim, ptm1, ptm2, CV_RGB(0,255,0), 1);
}
 for( ; contour != 0; contour = contour->h_next ){
   bndRect = cvBoundingRect(contour, 0);
   pt1.x = bndRect.x;
   pt1.y = bndRect.y;
   pt2.x = bndRect.x + bndRect.width;
   pt2.y = bndRect.y + bndRect.height;
   
   if((((mam1.x>=(pt1.x-w/2) && mam1.x<=(pt2.x+w/2)) || (mam2.x<=(pt2.x+w/2) && mam2.x>=(pt1.x+w/2))) && (mam1.y>=(pt1.y-h/2) && mam2.y<=(pt2.y+h/2) )) && abs(pt1.x-pt2.x)>abs(ma1.x-ma2.x)){
   ma1.x=pt1.x;
   ma2.x=pt2.x;
      //   if((mam1.y>=(pt1.y-h/2) || mam2.y<=(pt2.y+h/2)) && abs(pt1.y-pt2.y)>abs(ma1.y-ma2.y)){
            ma1.y=pt1.y;
            ma2.y=pt2.y;
        // }
   }


}  

   IplImage* crop = cvCreateImage(cvSize(abs(ma2.x-ma1.x),abs(ma2.y-ma1.y)), IPL_DEPTH_8U, 3);
   IplImage* gri = cvCreateImage(cvSize(92,112), IPL_DEPTH_8U, 1);
   IplImage* boyut = cvCreateImage(cvSize(92,112), IPL_DEPTH_8U, 3);
   printf("Buyuk:\n%d %d\n%d %d\n\n",ma1.x,ma1.y,ma2.x,ma2.y);
   printf("Buyukm:\n%d %d\n%d %d\n\n",mam1.x,mam1.y,mam2.x,mam2.y);
 //  cvRectangle(resim, ma1, ma2, CV_RGB(255,0,0), 1);
 //  cvRectangle(resim, mam1, mam2, CV_RGB(0,0,255), 1);
 //  cvNamedWindow("My Window1", CV_WINDOW_AUTOSIZE);
 //  cvShowImage("My Window1",resim);
   
   printf("B:\n%d %d\n\n",abs(ma2.x-ma1.x),abs(ma2.y-ma1.y));
   cvNamedWindow("My Window1", CV_WINDOW_AUTOSIZE);
   cvShowImage("My Window1",resim);
   w=resim->width;
   h=resim->height;
   mRect = cvRect(ma1.x,ma1.y,abs(ma2.x-ma1.x),abs(ma2.y-ma1.y));
   cvSetImageROI(resim,mRect);
 //  mRect = cvRect(0,0,64,48);
 //  cvSetImageROI(resim,mRect);
   cvCopy(resim,crop,0);
   cvSaveImage("crop.jpg",crop);

   IplImage * ye;
   ye=resz(crop);
   printf("%d %d",ye->width,ye->height); 
   cvNamedWindow("My Window", CV_WINDOW_AUTOSIZE);
   cvShowImage("My Window",ye);
   
    cvWaitKey(20000);


    cvReleaseImage(&gr);
    cvReleaseImage(&crop);
    cvReleaseImage(&resim);
 //   cvDestroyWindow("My Window");
    cvFree(&storage);
    cvDestroyWindow("My Window1");
    cvDestroyWindow("My Window");
    system("pause");
     return 0;
    }
   
IplImage * resz(IplImage * kes){
         //*******************************************************************************************
    CvScalar s,s1,gr;
    int i,j,gs;
    float w,h;
   CvMemStorage* storage = cvCreateMemStorage(0);
   CvSeq* contour = 0;
   GOZ g[5],l,r;
    
    IplImage* ycb = cvCreateImage(cvSize(kes->width,kes->height), IPL_DEPTH_8U, 3);
    IplImage* son2 = cvCreateImage(cvSize(kes->width,kes->height), IPL_DEPTH_8U, 1);
    IplImage* eyemapc = cvCreateImage(cvSize(kes->width,kes->height), IPL_DEPTH_8U, 1);


    cvCvtColor(kes,ycb,CV_BGR2YCrCb);    
   
    CvMat *cb = cvCreateMat( kes->height, kes->width, CV_32FC1);
    CvMat *cr = cvCreateMat( kes->height, kes->width, CV_32FC1);
    CvMat *cbcr = cvCreateMat( kes->height, kes->width, CV_32FC1);
    CvMat *gri = cvCreateMat( kes->height, kes->width, CV_32FC1);
                       

    for(i=0;i<(ycb->height);i++){
      for(j=0;j<(ycb->width);j++){
        s=cvGet2D(ycb,i,j);

        cvmSet(gri,i,j,s.val[0]);
        cvmSet(cb,i,j,pow((int)s.val[2],2));
        cvmSet(cr,i,j,pow((int)(255-s.val[1]),2));
        cvmSet(cbcr,i,j,(float)(s.val[2]/s.val[1]));
      }
    }
    
    cvNormalize(cb,cb,0,255, CV_MINMAX, 0);
    cvNormalize(cr,cr,0,255, CV_MINMAX, 0);
    cvNormalize(cbcr,cbcr,0,255, CV_MINMAX, 0);
    
    for(i=0;i<(ycb->height);i++){
      for(j=0;j<(ycb->width);j++){
      s1.val[0]=(cvmGet(cb,i,j)+cvmGet(cr,i,j)+cvmGet(cbcr,i,j))/3;
      cvSet2D(eyemapc,i,j,s1);
      }
    }
   cvEqualizeHist( eyemapc,eyemapc );
   
   CvRect bndRect = cvRect(0,0,0,0);
   CvPoint pt1, pt2,pt3,pt4;

   //cvSaveImage("eyemapc1.jpg",eyemapc);
   
   cvThreshold(eyemapc, eyemapc, 150, 255, CV_THRESH_BINARY);
   cvDilate(eyemapc,eyemapc,NULL,1);
   cvSaveImage("eyemapc.jpg",eyemapc);
  // cvSaveImage("eyemapc.jpg",eyemapc);
   i=0;
   cvFindContours( eyemapc, storage, &contour, sizeof(CvContour), CV_RETR_CCOMP, CV_CHAIN_APPROX_SIMPLE );
   for( ; contour != 0; contour = contour->h_next ){
   bndRect = cvBoundingRect(contour, 0);
   pt1.x = bndRect.x;
   pt1.y = bndRect.y;
   pt2.x = bndRect.x + bndRect.width;
   pt2.y = bndRect.y + bndRect.height;
   
   if(pt1.x>5 && pt2.x<kes->width-5 && pt1.y<kes->height/2 && pt1.y>10 && (bndRect.width*bndRect.height)>80){
    if(i<5){
      g[i].x=pt1.x;
      g[i].y=pt1.y;
      g[i].width=bndRect.width;
      g[i].height=bndRect.height;
      printf("%d %d\n",g[i].width,g[i].height); 
      i++;
  //cvRectangle(kes, pt1, pt2, CV_RGB(0,255,0), 1);
     printf("%d %d %d %d\n",pt1.x,pt1.y,pt2.x,pt2.y);
    }
   }
 }
printf("kare sayisi:%d\n",i);
gs=i;
  if(i==0){
   printf("Goz Yok!\n");
  }else{
 
     while(i>0){
       i=i-1;
       if(g[i].x<=kes->width/1.5 && g[i].x<r.x){
         r=g[i];
          if(gs==1){
            l.width=-1;
          }
       }
     }
      
      i=gs; 
      if(l.width!=-1){
       while(i>0){
        i=i-1;
       if(g[i].x>r.x && abs(r.y-g[i].y)<15 && (abs(r.x-g[i].x)<kes->width*0.6)){
         l=g[i];
         }
       }
      }
  }
  if(l.width>kes->width){
  l.width=-1;

  }
  if(r.width>kes->width){
  r.width=-1;
  }
  IplImage* dst;
  if(l.width!=-1 && r.width!=-1){

  dst=cvCreateImage(cvSize(kes->width*((float)20/abs(l.x-(r.x+r.width))),kes->height*((float)20/abs(l.x-(r.x+r.width)))), IPL_DEPTH_8U, 3);
  
  cvResize(kes,dst);
  
  }else {
   dst=cvCloneImage(kes);
  
  }
    CvRect mRect;
  if(l.width!=-1 && r.width!=-1){
  w=(float)(l.x+l.width+40-(r.x-40))*((float)20/abs(l.x-(r.x+r.width)));
  h=(float)(r.y+90-(r.y-60))*((float)20/abs(l.x-(r.x+r.width)));
     printf("fark: %f %f %d %d\n",w,h,dst->width,dst->height);
 //  if((int)w<=dst->width && (int)h<=dst->height){

    mRect = cvRect(r.x-40,r.y-60,(int)w,(int)h);
    cvSetImageROI(dst,mRect);  
 //  }
  }
  printf("right:%d %d\nleft:%d %d\n",r.width,r.height,l.width,l.height);
pt3.x=r.x;
pt3.y=r.y;
pt4.x=r.x+r.width;
pt4.y=r.y+r.height;
cvRectangle(kes, pt3, pt4, CV_RGB(255,0,0), 1);
pt3.x=l.x;
pt3.y=l.y;
pt4.x=l.x+l.width;
pt4.y=l.y+l.height;
cvRectangle(kes, pt3, pt4, CV_RGB(0,0,255), 1);

   cvSaveImage("goz.jpg",dst);    


   cvDestroyWindow("My Window");
  cvReleaseImage(&kes);
 cvReleaseImage(&ycb);
 cvFree(&storage);
 cvReleaseImage(&son2);
  cvReleaseImage(&eyemapc);
return dst;
 
         //*******************************************************************************************
}

